package com.gx.smart.building.ui.assets.fragment

import android.content.Intent
import android.view.ViewGroup
import com.gx.smart.base.BaseAdapter.OnItemClickListener
import com.gx.smart.base.BaseMVVMRefreshFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentAssetsInspectionRecordBinding
import com.gx.smart.building.ui.assets.AssetsInspectionDetailActivity
import com.gx.smart.building.ui.assets.adapter.InspectionRecordAdapter
import com.gx.smart.building.ui.assets.viewmodel.AssetsViewModel
import com.huishi.gx.ib.grpc.lib.ib.asset.asset.v1.PlanRecordDto
import com.scwang.smartrefresh.layout.api.RefreshLayout

class AssetsInspectionRecordFragment : BaseMVVMRefreshFragmentV2<AssetsViewModel>(),
    OnItemClickListener<PlanRecordDto> {

    private lateinit var adapter: InspectionRecordAdapter
    private lateinit var viewBinding: FragmentAssetsInspectionRecordBinding
    private var isFirstLoad = true
    override fun onBindLayout(): Int = R.layout.fragment_assets_inspection_record

    override fun getToolbarTitle(): String? = ""

    override fun enableToolbar(): Boolean = false

    override fun onBindViewModel(): Class<AssetsViewModel> = AssetsViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentAssetsInspectionRecordBinding.inflate(layoutInflater, root, true)
        adapter = InspectionRecordAdapter(activity)
        adapter.setItemClickListener(this)
        initView(viewBinding.recyclerView, adapter, viewBinding.refreshLayout)
    }

    override fun onRefresh(refreshlayout: RefreshLayout) {
        super.onRefresh(refreshlayout)
        initData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        super.onLoadMore(refreshLayout)
        initData()
    }

    override fun initData() {
        super.initData()
        val id = requireActivity().intent.getStringExtra("id")
        viewModel.queryInspectionRecordList(this.currentPage, id!!).observe(viewLifecycleOwner) {
            handleResult(it)
        }
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            viewBinding.refreshLayout.autoRefresh()
            isFirstLoad = false
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() = AssetsInspectionRecordFragment()
    }

    override fun onItemClick(e: PlanRecordDto, position: Int) {
        val intent = Intent(requireContext(), AssetsInspectionDetailActivity::class.java)
        intent.putExtra("assetId", e.assetId)
        intent.putExtra("planTaskId", e.planTaskId)
        startActivity(intent)
    }

}